﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using Lib.Data;
using Lib.Utils;

namespace SysObj
{
    public class Bg_KhuVuc : Lib.Bases.BaseClass
    {
        Bg_KhuVuc_DoiTuong obj = new Bg_KhuVuc_DoiTuong();
        Bg_KhuVuc_GiaLe pri = new Bg_KhuVuc_GiaLe();

        #region Prop
        public System.Int32 IdGiaKhuVuc
        {
            get;
            set;
        }

        public System.DateTime TuNgay
        {
            get;
            set;
        }

        public System.DateTime DenNgay
        {
            get;
            set;
        }

        public System.Boolean bool_TuNgay
        {
            get;
            set;
        }

        public System.Int32 bool_DoiTuong
        {
            get;
            set;
        }

        public System.Int32 bool_GiaThucDon
        {
            get;
            set;
        }

        public System.Int32 GiaKhuVuc_PT
        {
            get;
            set;
        }

        public System.Decimal GiaKhuVuc_Tien
        {
            get;
            set;
        }

        public System.Int32 bool_GiaThucDon_Loai
        {
            get;
            set;
        }

        #endregion

        public DataTable Locations { get; set; }
        public DataTable Prices { get; set; }

        public Bg_KhuVuc()
        {
            m_DataLayer = new Lib.Data.DataLayer();
        }

        #region Methods
        public override void ResetData()
        {
            IdGiaKhuVuc = 0;
            TuNgay = DateTime.Now;
            DenNgay = DateTime.Now;
            bool_TuNgay = true;
            bool_DoiTuong = 1;
            bool_GiaThucDon = 1;
            GiaKhuVuc_PT = 0;
            GiaKhuVuc_Tien = 0;
            bool_GiaThucDon_Loai = 0;
        }

        public override DataTable LoadData()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = m_DataLayer.ExecDataTable("SP_Bg_KhuVuc", " Command,IdGiaKhuVuc", "LoadData", IdGiaKhuVuc);
                this.ResetData();
                if (dt == null || dt.Rows.Count <= 0)
                    return null;
                this.FillData(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public override void FillData(DataRow row)
        {
            ResetData();
            if (row.Table.Columns.Contains("IdGiaKhuVuc"))
                IdGiaKhuVuc = Lib.Data.ValueType.GetValue(row["IdGiaKhuVuc"], IdGiaKhuVuc);
            if (row.Table.Columns.Contains("TuNgay"))
                TuNgay = Lib.Data.ValueType.GetValue(row["TuNgay"], TuNgay);
            if (row.Table.Columns.Contains("DenNgay"))
                DenNgay = Lib.Data.ValueType.GetValue(row["DenNgay"], DenNgay);
            if (row.Table.Columns.Contains("bool_TuNgay"))
                bool_TuNgay = Lib.Data.ValueType.GetValue(row["bool_TuNgay"], bool_TuNgay);
            if (row.Table.Columns.Contains("bool_DoiTuong"))
                bool_DoiTuong = Lib.Data.ValueType.GetValue(row["bool_DoiTuong"], bool_DoiTuong);
            if (row.Table.Columns.Contains("bool_GiaThucDon"))
                bool_GiaThucDon = Lib.Data.ValueType.GetValue(row["bool_GiaThucDon"], bool_GiaThucDon);
            if (row.Table.Columns.Contains("GiaKhuVuc_PT"))
                GiaKhuVuc_PT = Lib.Data.ValueType.GetValue(row["GiaKhuVuc_PT"], GiaKhuVuc_PT);
            if (row.Table.Columns.Contains("GiaKhuVuc_Tien"))
                GiaKhuVuc_Tien = Lib.Data.ValueType.GetValue(row["GiaKhuVuc_Tien"], GiaKhuVuc_Tien);
            if (row.Table.Columns.Contains("bool_GiaThucDon_Loai"))
                bool_GiaThucDon_Loai = Lib.Data.ValueType.GetValue(row["bool_GiaThucDon_Loai"], bool_GiaThucDon_Loai);
        }

        public DataTable Location_LoadByLocation()
        {
            return obj.LoadByLocation(m_DataLayer, IdGiaKhuVuc);
        }

        public DataTable Location_LoadByTable()
        {
            return obj.LoadByTable(m_DataLayer, IdGiaKhuVuc);
        }

        public DataTable Price_LoadByGroup()
        {
            return pri.LoadByGroup(m_DataLayer, IdGiaKhuVuc);
        }

        public DataTable Price_LoadByMenu()
        {
            return pri.LoadByMenu(m_DataLayer, IdGiaKhuVuc);
        }

        public override void OnUpdate()
        {
            Hashtable output = null;
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Update");
                Lib.Data.ParamHolder.AddParam("IdGiaKhuVuc", IdGiaKhuVuc);
                Lib.Data.ParamHolder.AddParam("TuNgay", Cvert.ToShortDate(TuNgay));
                Lib.Data.ParamHolder.AddParam("DenNgay", Cvert.ToShortDate(DenNgay));
                Lib.Data.ParamHolder.AddParam("bool_TuNgay", bool_TuNgay);
                Lib.Data.ParamHolder.AddParam("bool_DoiTuong", bool_DoiTuong);
                Lib.Data.ParamHolder.AddParam("bool_GiaThucDon", bool_GiaThucDon);
                Lib.Data.ParamHolder.AddParam("GiaKhuVuc_PT", GiaKhuVuc_PT);
                Lib.Data.ParamHolder.AddParam("GiaKhuVuc_Tien", GiaKhuVuc_Tien);
                Lib.Data.ParamHolder.AddParam("bool_GiaThucDon_Loai", bool_GiaThucDon_Loai);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_BG_KHUVUC", s_ParamName, ref output, o_ParamValue);

                if (!object.Equals(null, output))
                    IdGiaKhuVuc = Cvert.ToInt32(output["IdGiaKhuVuc"], 0);

                obj.OnDelete(m_DataLayer, IdGiaKhuVuc);
                obj.Insert(m_DataLayer, Locations, IdGiaKhuVuc);

                pri.OnDelete(m_DataLayer, IdGiaKhuVuc);
                pri.Insert(m_DataLayer, Prices, IdGiaKhuVuc);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        public override void OnDelete()
        {
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                obj.OnDelete(m_DataLayer, IdGiaKhuVuc);
                pri.OnDelete(m_DataLayer, IdGiaKhuVuc);

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Delete");
                Lib.Data.ParamHolder.AddParam("IdGiaKhuVuc", IdGiaKhuVuc);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_BG_KHUVUC", s_ParamName, o_ParamValue);

                m_DataLayer.CommitTransaction(); 
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }


        #endregion
    }
}
